package heima3_2.cn.osc.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.oschina.app.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yaozhanlie on 2017/1/15.
 */

public abstract class OurTabBaseActivity extends AppCompatActivity {
    @Bind(R.id.btn_back)
    ImageButton mBtnBack;
    @Bind(R.id.tv_title)
    TextView mTvTitle;
    @Bind(R.id.iv_refresh)
    protected ImageView mIvRefresh;
    @Bind(R.id.tabs)
    TabLayout mTabs;
    @Bind(R.id.vp)
    ViewPager mVp;
    @Bind(R.id.activity_main)
    LinearLayout mActivityMain;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_tab_viewpager);
        ButterKnife.bind(this);
        initView();

    }

    protected void initView(){
        mTvTitle.setText(getTitleText());

        mIvRefresh.setVisibility(View.INVISIBLE);
        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initAdapter();
    }

    protected void initAdapter(){
    //使用FragmentpagerAdapter
    UserCollectAdapter businessFrgamentAdapter = new UserCollectAdapter(getSupportFragmentManager(),getTitleArray());
    businessFrgamentAdapter.setFragmentList(getFragmentList());  //在适配器中用个set方法让外界传数据进去
    mVp.setAdapter(businessFrgamentAdapter);    //设置适配器
    mTabs.setupWithViewPager(mVp);  //tabLayout跟ViewPager的绑定
    }

    /**子类需要将TabLayout的标题数组传进来*/
    protected abstract String[] getTitleArray();

    /**子类需要重写这个方法，将所对应的Fragment集合返回回来*/
    protected abstract List<Fragment> getFragmentList();

    /**给子类提供一个方法，设置界面的标题*/
    protected abstract String getTitleText();

}
